//
//  DDLauchViewController.m
//  DDMusic
//
//  Created by rimi on 16/11/4.
//  Copyright © 2016年 FengBaoFamily. All rights reserved.
//


#import "DDLauchViewController.h"
#import "DrawCircleProgressButton.h"
#import "DDPublicMacro.h"
#import <AVOSCloud.h>
#import "DDmediaPlayer.h"
#import "DDMainTabBarController.h"
#import "CollapseAnimator.h"
@interface DDLauchViewController ()<UIViewControllerTransitioningDelegate>
@property (nonatomic,strong)UIImageView* lauchImageView;//启动欢迎界面图片
@property (nonatomic,strong)DrawCircleProgressButton* progressBtn;//启动欢迎界面进度控件
@property (nonatomic,assign)BOOL isNetWorkMusic;//用来记录是否是网络加载的歌曲

@end

@implementation DDLauchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self layoutUI];
    [self loadImageAndMusic];

  }
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
#pragma mark UI设置
- (void)layoutUI{
   
//lauchImageView
    self.view.backgroundColor = [UIColor whiteColor];
    self.lauchImageView  = [[UIImageView alloc]init];
    self.lauchImageView.frame = CGRectMake(0, 0, SCREEN_SIZE.width,SCREEN_SIZE.height);
    self.lauchImageView.userInteractionEnabled = YES;
    [self.lauchImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapLauchImageView:)]];
      [self.view addSubview:self.lauchImageView];
//progressBtn
    self.progressBtn = [[DrawCircleProgressButton alloc]initWithFrame:CGRectMake(SCREEN_SIZE.width-65,30, 45, 45)];
    [self.view addSubview:self.progressBtn];
     self.progressBtn.lineWidth = 2;
    [self.progressBtn setTitle:@"跳过" forState:(UIControlStateNormal)];
    self.progressBtn.showsTouchWhenHighlighted = YES;
    [self.progressBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    self.progressBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    self.progressBtn.userInteractionEnabled = YES;
     [self.progressBtn addTarget:self action:@selector(removeProgress) forControlEvents:UIControlEventTouchUpInside];
    

    
    
}

#pragma mark 逻辑操作部分
#pragma mark 加载视图和歌曲
- (void)loadImageAndMusic{
#pragma mark 加载网络下好的歌曲还是本地的歌曲
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *imagePath = [[paths objectAtIndex:0]stringByAppendingPathComponent:[[NSUserDefaults standardUserDefaults] valueForKey:LAUNCHMUSICIMAGENAME]];
    NSString *musicPath = [@"//" stringByAppendingString:[[paths objectAtIndex:0]stringByAppendingPathComponent:[[NSUserDefaults standardUserDefaults] valueForKey:LAUNCHMUSICURLNAME]]];
//利用NSFileManager对象操作文件
    if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath] && [[NSFileManager defaultManager] fileExistsAtPath:musicPath] && [[NSUserDefaults standardUserDefaults] valueForKey:LAUNCHMUSICIMAGENAME]!=nil,[[NSUserDefaults standardUserDefaults] valueForKey:LAUNCHMUSICURLNAME]!=nil) {
        self.lauchImageView.image = [UIImage imageWithContentsOfFile:imagePath];
        CGFloat beginTime = [(NSNumber*)[[NSUserDefaults standardUserDefaults]valueForKey:LAUNCHMUSICBEGINTIME] floatValue];
        
        [[DDmediaPlayer shareDDmediaPlayer]nativePlacemusicPlayerWithName:musicPath beginTime:beginTime];
        self.isNetWorkMusic = YES;
    }else{
        self.lauchImageView.image = [UIImage imageNamed:@"lunchImage"];
        [[DDmediaPlayer shareDDmediaPlayer]nativePlacemusicPlayerWithName: [[NSBundle mainBundle]pathForResource:@"mp3_王真-雨梦.mp3" ofType:nil] beginTime:0];
        self.isNetWorkMusic = NO;
        
        
    }
    
    
    /**
     *  progress 完成时候的回调
     */
    CGFloat musicContinueTime = 0;
    if (self.isNetWorkMusic ==YES) {
        musicContinueTime = [(NSNumber*)[[NSUserDefaults standardUserDefaults]valueForKey:LAUNCHMUSICCONTINUTIME] floatValue];
    }else{
        musicContinueTime = 9.5;
    }
    __weak DDLauchViewController *weakSelf = self;
    [self.progressBtn startAnimationDuration:musicContinueTime withBlock:^{
        [weakSelf removeProgress];
    }];
    




}
#pragma mark 进度按钮的回调方法
- (void)removeProgress
{
    if (self.progressBtn.selected == YES) {
        return;
    }
    self.progressBtn.selected = YES;
    [[DDmediaPlayer shareDDmediaPlayer].audioPlayer stop];
    DDMainTabBarController* mainTabBarVC = [[DDMainTabBarController alloc]init];
    mainTabBarVC.transitioningDelegate = self;
    [self presentViewController:mainTabBarVC animated:YES completion:nil];
 
    

}
#pragma mark 歌曲图片点击手势回调
//点击跳转到推荐歌曲的播放界面播放歌曲
- (void)tapLauchImageView:(UITapGestureRecognizer*)tapGes{
    
    
}
#pragma mark 隐藏系统的状态栏
- (BOOL)prefersStatusBarHidden{
    return YES;
    
}
#pragma mark 跳转页面的动画
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
     NSLog(@"%@",self.viewModel);
CollapseAnimator *animator = [[CollapseAnimator alloc] init];
    return animator;
    

}

@end
